🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / core / database / src / commonMain / kotlin / org / meshtastic / core / database / BusyTimeoutSQLiteDriver.kt
Displaying Raw • Download
core/database/src/commonMain/kotlin/org/meshtastic/core/database/BusyTimeoutSQLiteDriver.kt bd2863243bab6eb213401d949839a2bc74dde7e2 (bd286324) Text, 3.36 KB
T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.database
Tff7b72import T7ee787androidx.sqlite.SQLiteConnection
Tff7b72import T7ee787androidx.sqlite.SQLiteDriver
Tff7b72import T7ee787androidx.sqlite.execSQL
T8b949e/**
* Wraps a [SQLiteDriver] so every connection it opens waits up to [busyTimeoutMs] for a competing connection's lock
* instead of failing immediately with SQLITE_BUSY ("Error code: 5, message: database is locked").
*
* Each database normally holds a single connection (see `configureCommon`), but [DatabaseManager]'s wedge recovery
* deliberately abandons a stalled connection and opens a replacement pool against the same file (see
* `abandonWedgedDbBlock`). Until the abandoned callback finishes, both connections are live — and the bundled driver's
* default busy timeout is zero, so any write on the replacement fails the moment the abandoned connection holds the
* write lock. In the field (2.8.1, build 29321949) that surfaced as a fatal uncaught SQLITE_BUSY from Room's own
* invalidation-tracker housekeeping (`TriggerBasedInvalidationTracker.syncTriggers`), which the app cannot catch. A
* busy timeout makes the replacement connection wait out the overlap instead.
*/
Tff7b72class T56d364BusyTimeoutSQLiteDriverTb4b4b4(
Tff7b72private Tff7b72val Te6edf3delegateTb4b4b4: Te6edf3SQLiteDriverTb4b4b4,
Tff7b72private Tff7b72val Te6edf3busyTimeoutMsTb4b4b4: Tffa657Long Tff7b72= Te6edf3DEFAULT_BUSY_TIMEOUT_MSTb4b4b4,
Tb4b4b4) Tb4b4b4: Te6edf3SQLiteDriver Tb4b4b4{
Tff7b72init Tb4b4b4{
T8b949e// SQLite disables the busy handler entirely for zero or negative values, which would silently
T8b949e// defeat this wrapper's whole purpose.
Te6edf3requireTb4b4b4(Te6edf3busyTimeoutMs Tff7b72> T79c0ff0Tb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffbusyTimeoutMs must be positive, was Tffd700$Te6edf3busyTimeoutMsTa5d6ff" Tb4b4b4}
Tb4b4b4}
Tff7b72override Tff7b72fun Td2a8ffopenTb4b4b4(Te6edf3fileNameTb4b4b4: Tffa657StringTb4b4b4)Tb4b4b4: Te6edf3SQLiteConnection Tb4b4b4{
Tff7b72val Te6edf3connection Tff7b72= Te6edf3delegateTb4b4b4.Te6edf3openTb4b4b4(Te6edf3fileNameTb4b4b4)
Tff7b72try Tb4b4b4{
Te6edf3connectionTb4b4b4.Te6edf3execSQLTb4b4b4(Ta5d6ff"Ta5d6ffPRAGMA busy_timeout = Tffd700$Te6edf3busyTimeoutMsTa5d6ff"Tb4b4b4)
Tb4b4b4} Tff7b72catch Tb4b4b4(Tf0883e@SuppressTb4b4b4(Ta5d6ff"Ta5d6ffTooGenericExceptionCaughtTa5d6ff"Tb4b4b4) Te6edf3setupFailureTb4b4b4: Te6edf3ThrowableTb4b4b4) Tb4b4b4{
T8b949e// The platforms throw different exception types here (android.database.SQLException on
T8b949e// Android, androidx.sqlite.SQLiteException elsewhere); close the live native connection
T8b949e// before rethrowing so a failed setup never leaks it.
Te6edf3connectionTb4b4b4.Te6edf3closeTb4b4b4(Tb4b4b4)
Tff7b72throw Te6edf3setupFailure
Tb4b4b4}
Tff7b72return Te6edf3connection
Tb4b4b4}
Tff7b72companion Tff7b72object Tb4b4b4{
T8b949e/**
* Long enough to ride out the typical abandoned-writer overlap (slow MeshLog cleanups and node-heavy packet
* transactions observed in the field run 1-30s), short enough that a truly stuck holder still surfaces as an
* error rather than an ANR-adjacent stall. Waiting happens on Room's I/O dispatcher, never the main thread.
*/
Tff7b72const Tff7b72val Te6edf3DEFAULT_BUSY_TIMEOUT_MS Tff7b72= T79c0ff1T79c0ff0Te6edf3_000L
Tb4b4b4}
Tb4b4b4}
Served by rngit 1.5.1 - Generated in 0.04s